<template>
  <div class="line-chart">
    <div class="chart">
      <chart
        ref="chartRef"
        :option="refChartOption"
        :style="{ height: chartHeight }"
        @zrender:click="renderClickHandle"
      />
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed, PropType, watch, onMounted } from 'vue';
import { Chart } from '@/components';
export interface TimelineItem {
  [key: string]: string | number;
}

export default defineComponent({
  props: {
    data: {
      type: Array as PropType<TimelineItem[]>,
      default: () => [],
    },
    fieldNames: {
      type: Object,
      default: () => ({
        name: 'name',
        values: ['value'],
      }),
    },
    height: {
      type: Number,
      default: 300,
    },
    legend: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  setup(props, { emit }) {
    const chartRef = ref<any>();

    const legend = computed(() => {
      let arr = [
        {
          name: '累计下降率',
          icon: 'rect',
        },
        {
          name: '警情数量',
          icon: 'line',
        },
      ];
      return arr;
    });
    const chartOption = {
      color: ['#1790ff', '#12c2c2', '#ee6666'],
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
          crossStyle: {
            color: '#999',
          },
        },
      },
      legend: {
        data: legend,
      },
      xAxis: {
        type: 'category',
        data: [] as string[],
        axisPointer: {
          type: 'shadow',
        },
      },
      yAxis: [
        {
          type: 'value',
          name: legend.value[0].name,
          min: 0,
          max: 250,
          interval: 50,
          axisLabel: {
            formatter: '{value}',
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
        },
        {
          type: 'value',
          name: legend.value[1].name,
          min: 0,
          max: 25,
          interval: 5,
          axisLabel: {
            formatter: '{value}',
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
        },
      ],
      dataZoom: {
        start: 0,
        end: 20,
      },
      series: [
        {
          name: legend.value[0].name,
          type: 'bar',
          barWidth: '30',
          data: [] as number[],
        },
        {
          name: legend.value[1].name,
          type: 'line',
          data: [] as number[],
        },
      ],
    };

    const refChartOption = ref(chartOption);

    const chartHeight = computed(() => `${props.height}px`);

    const setChartOption = (data: any[]) => {
      const xAxis: string[] = [];
      interface SeriesDataMap {
        [key: string]: Array<any>;
      }
      const seriesDataMap: SeriesDataMap = {};
      data.forEach(item => {
        const name = item[props.fieldNames.name];
        props.fieldNames.values.forEach(value => {
          if (seriesDataMap[value]) {
            seriesDataMap[value].push(item[value]);
          } else {
            seriesDataMap[value] = [item[value]];
          }
        });
        xAxis.push(name);
      });
      refChartOption.value.xAxis.data = xAxis;
      Object.values(seriesDataMap).forEach((o, i) => {
        refChartOption.value.series[i].data = o;
      });
    };

    const renderClickHandle = e => {
      if (e?.topTarget?.type === 'Line') {
        const index = chartRef.value.getIndexByEvent(e);
        const item = props.data[index];
        emit('zrender:click', index, item);
      }
    };

    watch(
      () => props.data,
      () => {
        setChartOption(props.data);
      },
    );

    onMounted(() => {
      setChartOption(props.data);
    });
    return {
      chartHeight,
      refChartOption,
      renderClickHandle,
      chartRef,
    };
  },
  components: {
    Chart,
  },
});
</script>

<style lang="less" scoped></style>
